﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FifaChamps.API.Data
{
    public static partial class DataManager
    {
        public static int Add_Knockout(int stageid, string name)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_Knockout(stageid, name).First();
                return result.KnockoutID.Value;
            }
        }

        public static int Update_Knockout(int knockoutid, int stageid, string name)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Update_Knockout(knockoutid, stageid, name).First();
                return result.KnockoutID.Value;
            }
        }

        public static Knockout Get_Knockout(int knockoutid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Knockouts.Where(gr => gr.ID == knockoutid).First();
                return new Knockout(result.ID, result.StageID, result.Name);
            }
        }

        public static List<Knockout> Get_Knockouts(int stageid)
        {
            List<Knockout> list = new List<Knockout>();
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Knockouts.Where(gr => gr.StageID == stageid).ToList();
                result.ForEach(g => list.Add(new Knockout(g.ID, g.StageID, g.Name)));
            }

            return list;
        }

        public static int Add_KnockoutPlayer(int knockoutid, int playerID)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Add_KnockoutPlayer(knockoutid, playerID, default(bool?), default(int?), default(int?)).First();
                return result.KnockoutPlayerID.Value;
            }
        }

        public static KnockoutPlayer Get_KnockoutPlayer(int knockoutid, int playerID)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Player_Knockouts.Where(ps => ps.KnockoutID == knockoutid && ps.PlayerID == playerID).First();
                return Get_KnockoutPlayer(result.ID);
            }
        }

        public static KnockoutPlayer Get_KnockoutPlayer(int knockout_playerid)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Player_Knockouts.Where(ps => ps.ID == knockout_playerid).First();
                return new KnockoutPlayer(result.ID, result.KnockoutID, result.PlayerID, result.Advanced, result.GoalsFor, result.GoalsAgainst);
            }
        }

        public static List<KnockoutPlayer> Get_KnockoutPlayers(int knockoutid)
        {
            List<KnockoutPlayer> knockoutplayers = new List<KnockoutPlayer>();

            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Player_Knockouts.Where(pg => pg.KnockoutID == knockoutid).ToList();
                result.ForEach(gp => knockoutplayers.Add(new KnockoutPlayer(gp.ID, gp.KnockoutID, gp.PlayerID, gp.Advanced, gp.GoalsFor, gp.GoalsAgainst)));
            }

            return knockoutplayers;
        }

        public static int Update_KnockoutPlayer(int knockoutid, int playerID, bool? advanced, int goalsFor, int goalsAgainst)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.FCT_Player_Knockouts.Where(sp => sp.KnockoutID == knockoutid && sp.PlayerID == playerID).First().ID;
                return Update_KnockoutPlayer(result, advanced, goalsFor, goalsAgainst);
            }
        }

        public static int Update_KnockoutPlayer(int knockout_playerid, bool? advanced, int goalsFor, int goalsAgainst)
        {
            using (var db = DataManager.FifaDB())
            {
                var result = db.SP_Update_KnockoutPlayer(knockout_playerid, advanced, goalsFor, goalsAgainst).First();
                return result.KnockoutPlayerID.Value;
            }
        }

        public static List<KnockoutFixture> Get_KnockoutFixtures(int knockoutid)
        {
            Dictionary<int, KnockoutFixture> list = new Dictionary<int, KnockoutFixture>();

            using (var db = DataManager.FifaDB())
            {
                var result = (from pk in db.FCT_Player_Knockouts
                              join pkr in db.FCT_PlayerKnockout_FixtureResults
                              on pk.ID equals pkr.PlayerKnockoutID
                              join r in db.FCT_Fixture_Results
                              on pkr.FixtureResultID equals r.ID
                              join f in db.FCT_Fixtures
                              on r.FixtureID equals f.ID
                              where pk.KnockoutID == knockoutid
                              select new
                              {
                                  playerknockout_resultid = pkr.ID,
                                  playerknockoutid = pkr.PlayerKnockoutID,
                                  fixtureresultid = r.ID,
                                  resulttype = r.ResultTypeID,
                                  goalsfor = r.GoalsFor,
                                  goalsagainst = r.GoalsAgainst,
                                  fixtureid = f.ID,
                                  fixturename = f.FixtureName,
                                  home = r.Home
                              });

                foreach (var item in result.ToList())
                {
                    KnockoutFixture knockout_fixture;
                    if (list.ContainsKey(item.fixtureid))
                    {
                        knockout_fixture = list[item.fixtureid];
                    }
                    else
                    {
                        knockout_fixture = new KnockoutFixture(item.fixtureid, item.fixturename);
                        list.Add(item.fixtureid, knockout_fixture);
                    }

                    knockout_fixture.Results.Add(new KnockoutResult(item.playerknockout_resultid, item.playerknockoutid, item.fixtureresultid, item.resulttype, item.fixtureid, item.goalsfor, item.goalsagainst, item.home));
                }
            }

            return list.Values.ToList();
        }

        public static KnockoutFixture Get_KnockoutFixture(int fixtureID)
        {
            KnockoutFixture knockout_fixture = null;
            using (var db = DataManager.FifaDB())
            {
                var result = (from pk in db.FCT_Player_Knockouts
                              join pkr in db.FCT_PlayerKnockout_FixtureResults
                              on pk.ID equals pkr.PlayerKnockoutID
                              join r in db.FCT_Fixture_Results
                              on pkr.FixtureResultID equals r.ID
                              join f in db.FCT_Fixtures
                              on r.FixtureID equals f.ID
                              where f.ID == fixtureID
                              select new
                              {
                                  playerknockout_resultid = pkr.ID,
                                  playerknockoutid = pkr.PlayerKnockoutID,
                                  fixtureresultid = r.ID,
                                  resulttype = r.ResultTypeID,
                                  goalsfor = r.GoalsFor,
                                  goalsagainst = r.GoalsAgainst,
                                  home = r.Home,
                                  fixtureid = f.ID,
                                  fixturename = f.FixtureName
                              }).ToList();


                foreach (var item in result.ToList())
                {
                    if (knockout_fixture == null) knockout_fixture = new KnockoutFixture(item.fixtureid, item.fixturename);
                    knockout_fixture.Results.Add(new KnockoutResult(item.playerknockout_resultid, item.playerknockoutid, item.fixtureresultid, item.resulttype, item.fixtureid, item.goalsfor, item.goalsagainst, item.home));
                }
            }

            return knockout_fixture;
        }
    }
}
